package org.eduapp.trains.service.option.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.eduapp.trains.DAOException;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.dao.option.OptionDAO;
import org.eduapp.trains.model.options.ModelOptions;
import org.eduapp.trains.service.option.OptionService;
import org.eduapp.trains.utils.UtilsString;

/**
 * 
 * @author jeccy
 *
 */
public class OptionServiceImpl implements OptionService {
	
	private final static Logger logger = Logger.getLogger(OptionServiceImpl.class);
	
	private OptionDAO optionDAO;

	@Override
	public ModelOptions get(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			try {
				return optionDAO.get(id);
			} catch (DAOException e) {
				logger.error("get option by id" + id + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get option by id" + id + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		logger.info("empty option id found when getting option by id");
		return null;
	}

	@Override
	public List<ModelOptions> getByName(String optionName) throws ServiceException {
		if (UtilsString.isNotEmpty(optionName)) {
			try {
				return optionDAO.getByName(optionName);
			} catch (DAOException e) {
				logger.error("get order by option name[" + optionName + "],caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get order by option name[" + optionName + "],caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		logger.info("empty option name found when getting order by option name");
		return null;
	}

	@Override
	public boolean remove(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			ModelOptions model = get(id);
			if (model != null) {
				try {
					optionDAO.remove(model);
					return true;
				} catch (DAOException e) {
					logger.error("Remove options failed with id" + id + ",caused by:" + e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					logger.error("Remove options failed with id" + id + ",caused by:" + e.getMessage());
					throw (new ServiceException(e));
				}
			} else {
				logger.info("Remove options failed, id with " + id + "not found");
			}
		}
		logger.info("Remove options failed, empty id found");
		return false;
	}

	@Override
	public boolean saveOrUpdate(ModelOptions model) throws ServiceException {
		if (model != null) {
			try {
				optionDAO.saveOrUpdate(model);
				return true;
			} catch (DAOException e) {
				logger.error("SaveOrUpdate option failed with model" + model.toString() + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("SaveOrUpdate option failed with model" + model.toString() + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		}
		logger.info("SaveOrUpdate option failed, empty model found");
		return false;
	}

	public void setOptionDAO(OptionDAO optionDAO) {
		this.optionDAO = optionDAO;
	}

	public OptionDAO getOptionDAO() {
		return optionDAO;
	}

	public static Logger getLogger() {
		return logger;
	}

}
